package cn.tedu.egweibo1.controller;

import cn.tedu.egweibo1.mapper.WeiboMapper;
import cn.tedu.egweibo1.pojo.dto.WeiboDto;
import cn.tedu.egweibo1.pojo.entity.Weibo;
import cn.tedu.egweibo1.pojo.vo.UserVO;
import cn.tedu.egweibo1.pojo.vo.WeiboDetailVo;
import cn.tedu.egweibo1.pojo.vo.WeiboVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/weibo/")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;
    /**
     * 1.发布微博功能
     * @param weiboDto
     * @param session
     * @return
     */
    @PostMapping("insert")
    public int insert(@RequestBody WeiboDto weiboDto, HttpSession session){
        System.out.println("weiboDto = " + weiboDto);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {

            return 2;
        }
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDto,weibo);
        weibo.setUserId(userVO.getId());
        weibo.setCreated(new Date());
        weiboMapper.insert(weibo);
        return 1;
    }
    /**
     * 2.首页微博列表功能
     */
    @GetMapping("selectIndex")
    public List<WeiboVo> selectIndex(WeiboVo weiboVo){
        System.out.println("weibo = " + weiboVo);
        List<WeiboVo> weibos = weiboMapper.selectIndex(weiboVo);
        return weibos;
    }
    /**
     * 3.微博详情页面
     */
    @GetMapping("selectById")
    public WeiboDetailVo selectById(Long id){
        WeiboDetailVo weiboDetailVo = weiboMapper.selectById(id);
        return weiboDetailVo;
    }

}
